/*
 * Copyright (C) <2009>  <Matthew Dickinson>
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.pulsarwebdevelopment.concordance.plugins;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.*;
//import java.io.FileWriter;

/**
 * Creates a graphical version of UniqueBlanker
 * @author Matthew Dickinson
 * Last updated: 11/26/2008
 */
public class GraphicalBlanker implements Plugin, ActionListener{

	private final int MAX_LINE_LENGTH = 40;
	private File readFile;
	private JPanel finishPane = new JPanel(new FlowLayout());
	private JFrame window = new JFrame();
	private JScrollPane scroll;// = new JScrollPane();
	private JPanel canvas = new JPanel();
	private ArrayList<JTextField> blanks = new ArrayList<JTextField>();
	private ArrayList<String> words = new ArrayList<String>();
	private JLabel text;
	private JTextField blank;
	private int curLineLength = 0;
	private JPanel line = new JPanel();

	public void plug(PluginScreen screen){
		screen.setTitleText("Unique Word Blanker",
		"This plug-in will allow you to test your knowledge of unique words using a graphics window");
		screen.giveFileBrowser(FileBrowser.FileType.TEXT, "File of chapter to be blanked");
		screen.giveConcList("Concordance to be used for blanking");
	}

	public void go(Object[] args)
	{
		readFile = (File) args[0];
		int concIndex = (Integer) args[1];

		scroll = new JScrollPane(canvas);
		scroll.setPreferredSize(new Dimension(700, 420));
		window.setPreferredSize(new Dimension (720, 500));
		canvas.setLayout(new GridLayout(0,1));


		String testText;

		for(int i = 0; i < 50; i++){
			//last word
			if(i == 49){
				curLineLength = MAX_LINE_LENGTH;
			}
			
			testText = "Num = "+ i;
			handleWord(testText);
			//blank.setPreferredSize(new Dimension((testText.length() * 12), 25));

		}

		JButton cancel = new JButton("Cancel");
		cancel.setActionCommand("cancel");
		cancel.addActionListener(this);

		JButton finish = new JButton("Judge");
		finish.setActionCommand("go");
		finish.addActionListener(this);

		finishPane.add(cancel);
		finishPane.add(finish);

		window.add(scroll, BorderLayout.NORTH);
		window.add(finishPane, BorderLayout.EAST);
		window.pack();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setVisible(true);
	}

	private void judge(){
		System.out.println("Judged");
		JTextField cur;
		String curStr;
		for(int i = 0; i < blanks.size(); i++){
			cur = blanks.get(i);
			curStr = words.get(i);
			if(!cur.getText().equals(curStr)){
				cur.setForeground(Color.RED);
				cur.setToolTipText(curStr);
			}
		}
	}
	
	private void handleWord(String word){
		if(word.length() > 1){
			text = new JLabel(word);
			line.add(text);
			curLineLength += text.getText().length();
		}
		if(word.length() > 1){
			blank = new JTextField(word.length());
			words.add(word);
			blanks.add(blank);
			line.add(blank);
			curLineLength += blank.getColumns();
		}
		
		if(curLineLength > MAX_LINE_LENGTH){
			canvas.add(line);
			line = new JPanel();
			curLineLength = 0;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("go")){
			judge();
		}
	}
}
